package com.householditemtwo.admin.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.householditemtwo.common.entity.Designer;
import com.householditemtwo.common.entity.Product;
import com.householditemtwo.common.utils.DBManager;
import com.householditemtwo.common.utils.QueryDataResult;

/**
 * 用户DAO
 * @author admin
 *
 */
	public class StyDao {
			
		/**
		 * 查询设计师的总条数(分页)
		 * @return
		 */
			public int count(String desname) {
				DBManager db=new DBManager();
				String sql="select count(*) from designer ";
				if (desname !=null && desname !="" ) {
					sql+=" where desname like '%"+desname+"%'";
				}
				QueryDataResult qdr=db.executeQuery(sql);
			int total=0;
				try {
					while(qdr.rs.next()) {
					total=qdr.rs.getInt(1);
					}
				}catch(SQLException e) {
					e.printStackTrace();
				}
				//返回查询的结果
				return total;
			}
			
		/**
		 * 查询所有设计师
		 * @return
		 */
			public List<Designer> selectAllDesigner( int page, int pageSize,String desname) {
				DBManager db=new DBManager();
				String sql="select * from designer ";
						
					if (desname !=null && desname !="" ) {
							sql  +=" where desname like '%"+desname+"%'";
					}
					sql+= " order by desid limit "+pageSize*(page-1)+","+pageSize;
				QueryDataResult qdr=db.executeQuery(sql);
				Product emp=new Product();
				ArrayList<Designer> list=new ArrayList<Designer>();
				try {
					while(qdr.rs.next()) {
						Designer c=new Designer();
						c.setDesid(qdr.rs.getInt(1));
						c.setDesname(qdr.rs.getString(2));
						c.setDesgrade(qdr.rs.getString(3));
						c.setDespisture(qdr.rs.getString(4));
						list.add(c);
					}
				}catch(SQLException e) {
					e.printStackTrace();
				}
				//返回查询的结果
				return list;
			}
			
			
			/**
			 * 修改设计师
			 * @return
			 */
		
		public int update(Designer p) {
				//写sql
			String sql= String.format("update designer set desname='%s',"
					+ "desgrade='%s',despisture='%s'  where desid=%d",
					p.getDesname(),p.getDesgrade(),p.getDespisture(),p.getDesid());
					
				//执行sql
				return	DBManager.executeUpdate(sql);
			
			
		}
		/**
		 * 新增设计师
		 * @return
		 */

		public int insert(Designer p) {
			//拼接sql
			String sql= String.format("insert into designer(desname,desgrade,despisture)"
					+ " value('%s','%s','%s') ",
					p.getDesname(),p.getDesgrade(),p.getDespisture());
					
			//执行sql
		return	DBManager.executeUpdate(sql);
			
			
		}
	
		/**
		 * 删除设计师
		 * @return
		 */
			public int delete(int id) {
				//拼接sql
				String sql= "delete from designer  where desid="+id;
						
				//执行sql
			return	DBManager.executeUpdate(sql);
			}
	
}
